// pages/shop/shop.js
const app = getApp() // 获取app.js这个文件
const util= require('../../utils/util.js') // 导入util.js文件

Page({
    data: { // 定义变量
        indicatorDots: true,
        autoplay: true,
        interval: 3000,
        duration: 1000,
        text: "服务第一，为满足我校学生的便利及商品的质量，承诺：价格便宜，商品性价比高！！！",
        step: 1000, // 滚动速度
        distance: 100, // 初始滚动距离
        space: 300,
        imgUrls: [
            '../../images/公告1.png',
        ],
        goods:[],
    },
    detail(e) { 
        
        util.redirect({
            url: 'detail', 
            id: e.currentTarget.dataset.item.id,
            gname: e.currentTarget.dataset.item.gname,
            price: e.currentTarget.dataset.item.price,
            detail:e.currentTarget.dataset.item.detail,
            amount:e.currentTarget.dataset.item.amount,
            uid: e.currentTarget.dataset.item.uid,
            gid: e.currentTarget.dataset.item.id
        })
    },
    onLoad: function (options) {
        if(!wx.getStorageSync('admin')){
            wx.showToast({
                title: '账号未登录',
                icon: 'error',
                duration: 2500,
                success:function(){
                    setTimeout(function(){
                        util.redirect('login')
                    },2500)
                }
            })   
        }
        
        console.log(wx.getStorageSync('admin'))
        util.http('/wx/goodsquery',resp=>{
            this.data.goods = resp.data.records
            this.setData(this.data)
        })
    },
    onPullDownRefresh(){
        util.http('/wx/goodsquery',resp=>{
          this.data.goods = resp.data.records
          util.stopPullSetData(this)
        })
      },
    notice(e){
        util.redirect({
            url: 'notice',  
        }) 
    },
    onShow: function() {
        var that = this;
        var query = wx.createSelectorQuery();
        // 选择id
        query.select('#mjltest').boundingClientRect();
        query.exec(function(res) {
          var length = res[0].width;
          var windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
          that.setData({
            length: length,
            windowWidth: windowWidth,
            space:windowWidth
          });
          that.scrollling(); // 第一个字消失后立即从右边出现
        });
      },
      scrollling: function() {
        var that = this;
        var length = that.data.length; // 滚动文字的宽度
        var windowWidth = that.data.windowWidth; // 屏幕宽度
        var interval = setInterval(function() {
          var maxscrollwidth = length + that.data.space;
          var left = that.data.distance;
          if (left < maxscrollwidth) { // 判断是否滚动到最大宽度
            that.setData({
              distance: left + that.data.step
            })
          } else {
            that.setData({
              distance: 0 // 直接重新滚动
            });
            clearInterval(interval);
            that.scrollling();
          }
        }, that.data.interval);
      }
})